package com.ruoyi.worker.handler;

import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Maps;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.worker.constant.Constant;
import com.ruoyi.worker.domain.workAttendance.RzScheduleStaffPlan;
import org.apache.poi.ss.usermodel.DateUtil;

import java.util.*;

import static com.ruoyi.worker.constant.Constant.PLAN_KEY;

/**
 * 导入排班计划模板处理类 </p>
 *
 * @author Hush.
 * @since 2021/11/19 13:47
 */
public class SchedulingPlanMapImportHandler extends ExcelDataHandlerDefaultImpl<Map<String, Object>> {

    /**
     * 员工信息列
     */
    private final static int STAFF_INFO_COL = 9;

    /**
     * 列映射
     */
    private final static Map<String, String> COL_MAPPING = Maps.newHashMap();

    /**
     * 记录员工每天的排班内容
     */
    private List<Map<String, Object>> plan;


    static {
        //初始化列
        COL_MAPPING.put("员工id", "staffId");
        COL_MAPPING.put("员工姓名", "staffName");
        COL_MAPPING.put("身份证信息", "idCard");
        COL_MAPPING.put("岗位", "stationName");
        COL_MAPPING.put("班组id", "deptId");
        COL_MAPPING.put("班组名称", "deptName");
        COL_MAPPING.put("组别id", "groupId");
        COL_MAPPING.put("组别名称", "groupName");
        COL_MAPPING.put("区域名称", "areaName");
    }


    @Override
    public void setMapValue(Map<String, Object> map, String originKey, Object value) {
        if (map.size() < STAFF_INFO_COL) {
            map.put(getRealKey(originKey), value != null ? value.toString() : null);
        } else {
            //处理考勤列数据
            if (null == map.get(PLAN_KEY)) {
                plan = new ArrayList<>();
                map.put(PLAN_KEY, plan);
            }
            Map<String, Object> planDay = Maps.newHashMap();
            Date date = null;
            try {
                date = DateUtil.getJavaDate(Double.parseDouble(originKey));
            }catch (NumberFormatException e){
                date =DateUtils.parseDate(originKey);
            }
            planDay.put("date",date);
            planDay.put("plan",value != null ? value.toString() : null);
            plan.add(planDay);
        }
    }

    /**
     * 将排班表模板各列转为实体类的属性 {@link RzScheduleStaffPlan}
     *
     * @param originKey:中文表头
     * @return java.lang.String
     * @author Hush.
     * @since 2021/11/19 14:11
     */
    private String getRealKey(String originKey) {
        String property = COL_MAPPING.get(originKey);
        return StringUtils.isEmpty(property) ? originKey : property;
    }
}
